package saxion.speelveld.spaceengineer.view.tabs;

import java.util.Observable;
import java.util.Observer;

import saxion.speelveld.spaceengineer.GameScreen;
import saxion.speelveld.spaceengineer.R;
import saxion.speelveld.spaceengineer.model.Game;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class GameMenu extends LinearLayout implements Observer
{

	private Button btnBuildBuilding;

	private Button btnAchievement;

	private Button btnTrade;

	private Context parentContenxt;

	private Game game;

	public GameMenu(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		Initialize(context);
	}

	public GameMenu(Context context)
	{
		super(context);
		Initialize(context);
	}

	public void Initialize(Context context)
	{
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.gamemenu, this);

		btnBuildBuilding = (Button) findViewById(R.id.btnBuildBuilding);
		btnBuildBuilding.setOnClickListener(new BuildingClickie());
		parentContenxt = context;

		btnAchievement = (Button) findViewById(R.id.btnAchievement);
		btnAchievement.setOnClickListener(new AchievementClickie());

		btnTrade = (Button) findViewById(R.id.btnTrade);
		btnTrade.setOnClickListener(new TradeClickie());

		ImageButton btnAccept = (ImageButton) findViewById(R.id.ibAcceptBuild);
		btnAccept.setOnClickListener(new acceptBuildButton());
		ImageButton btnCancel = (ImageButton) findViewById(R.id.ibCancelBuild);
		btnCancel.setOnClickListener(new cancelBuildButton());

		Button buildinfra = (Button) findViewById(R.id.btnBuildinfra);
		ImageButton acptinfra = (ImageButton) findViewById(R.id.ibAcceptinfrBuild);
		ImageButton cancelinfra = (ImageButton) findViewById(R.id.ibCancelinfra);

		buildinfra.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				game.setBuildInfrastructure(true);
				findViewById(R.id.ibCancelinfra).setVisibility(VISIBLE);
				findViewById(R.id.ibAcceptinfrBuild).setVisibility(VISIBLE);
				findViewById(R.id.btnBuildinfra).setVisibility(GONE);
			}
		});
		acptinfra.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (!game.addinfrastructure())
				{
					if (((Activity) getContext()) instanceof GameScreen)
					{
						GameScreen a = (GameScreen) getContext();
						a.showInvalidBuildingSpotToast();
					}
				}
			}
		});
		cancelinfra.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				game.setBuildInfrastructure(false);
				findViewById(R.id.ibCancelinfra).setVisibility(GONE);
				findViewById(R.id.ibAcceptinfrBuild).setVisibility(GONE);
				findViewById(R.id.btnBuildinfra).setVisibility(VISIBLE);
			}
		});

	}

	public void setGameModel(Game gameModel)
	{
		this.game = gameModel;
		game.addObserver(this);
	}

	private class TradeClickie implements OnClickListener
	{

		@Override
		public void onClick(View arg0)
		{
			if (((Activity) getContext()) instanceof GameScreen)
			{
				GameScreen a = (GameScreen) getContext();
				a.showTradePopup();
			}

		}
	}

	private class BuildingClickie implements OnClickListener
	{

		@Override
		public void onClick(View arg0)
		{
			if (((Activity) getContext()) instanceof GameScreen)
			{
				GameScreen a = (GameScreen) getContext();
				a.showBuildingPopup();
			}

		}
	}

	private class AchievementClickie implements OnClickListener
	{

		@Override
		public void onClick(View arg0)
		{
			if (((Activity) getContext()) instanceof GameScreen)
			{
				GameScreen a = (GameScreen) getContext();
				a.showachievementScreen();
			}

		}
	}

	private class acceptBuildButton implements OnClickListener
	{

		@Override
		public void onClick(View v)
		{
			boolean result = game.AddBuilding(0, 0, false, false);
			if (result == false)
			{
				if (((Activity) getContext()) instanceof GameScreen)
				{
					GameScreen a = (GameScreen) getContext();
					a.showInvalidBuildingSpotToast();
				}
			}

		}
	}

	private class cancelBuildButton implements OnClickListener
	{
		@Override
		public void onClick(View v)
		{
			game.setQueuedBuilding(null);
		}
	}

	private void setButtonVisibility(int Visibility)
	{
		ImageButton btnAccept = (ImageButton) findViewById(R.id.ibAcceptBuild);
		ImageButton btnCancel = (ImageButton) findViewById(R.id.ibCancelBuild);
		btnAccept.setVisibility(Visibility);
		btnCancel.setVisibility(Visibility);
		if (Visibility == View.GONE)
		{
			btnBuildBuilding.setVisibility(View.VISIBLE);
		}
		else
		{
			btnBuildBuilding.setVisibility(View.INVISIBLE);
		}
	}

	@Override
	public void update(Observable model, Object arg1)
	{
		if (model instanceof Game)
		{
			Game gameModel = (Game) model;
			if (gameModel.getQueuedBuilding() != null)
			{
				setButtonVisibility(View.VISIBLE);
			}
			else
			{
				setButtonVisibility(View.GONE);
			}
			if (!game.isBuildInfrastructure())
			{
				findViewById(R.id.ibCancelinfra).setVisibility(GONE);
				findViewById(R.id.ibAcceptinfrBuild).setVisibility(GONE);
				findViewById(R.id.btnBuildinfra).setVisibility(VISIBLE);
			}

		}
	}
}
